#!/bin/bash

echo "开始编译 GB28181 客户端示例..."

# 检查操作系统
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" || "$OSTYPE" == "cygwin" ]]; then
    IS_WINDOWS=true
    echo "检测到Windows环境"
else
    IS_WINDOWS=false
    echo "检测到类Unix环境"
fi

# 尝试找出系统中可用的库
echo "检查系统库..."

# 检查pkg-config是否可用
if ! command -v pkg-config &> /dev/null; then
    echo "pkg-config命令不可用，将使用默认路径..."
    PKG_CONFIG_AVAILABLE=false
else
    PKG_CONFIG_AVAILABLE=true
    echo "pkg-config可用，使用它查找库路径..."
fi

# 准备CFLAGS和LIBS变量
CFLAGS="-std=c++11"
LIBS=""

# 尝试使用pkg-config获取OpenCV标志
if $PKG_CONFIG_AVAILABLE && pkg-config --exists opencv4 2>/dev/null; then
    echo "找到OpenCV4..."
    CFLAGS="$CFLAGS $(pkg-config --cflags opencv4)"
    LIBS="$LIBS $(pkg-config --libs opencv4)"
elif $PKG_CONFIG_AVAILABLE && pkg-config --exists opencv 2>/dev/null; then
    echo "找到OpenCV..."
    CFLAGS="$CFLAGS $(pkg-config --cflags opencv)"
    LIBS="$LIBS $(pkg-config --libs opencv)"
else
    echo "未找到OpenCV的pkg-config，使用默认包含路径..."
    CFLAGS="$CFLAGS -I/usr/include/opencv"
    LIBS="$LIBS -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_video -lopencv_plot -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core"
fi

# 检查FFmpeg组件
echo "检查FFmpeg组件..."
FFMPEG_COMPONENTS=("libavcodec" "libavformat" "libavutil" "libswscale")
for component in "${FFMPEG_COMPONENTS[@]}"; do
    if $PKG_CONFIG_AVAILABLE && pkg-config --exists $component 2>/dev/null; then
        echo "找到$component..."
        CFLAGS="$CFLAGS $(pkg-config --cflags $component)"
        LIBS="$LIBS $(pkg-config --libs $component)"
    else
        echo "未找到$component的pkg-config，使用默认包含路径..."
        CFLAGS="$CFLAGS -I/usr/include/ffmpeg"
        LIBS="$LIBS -l${component#lib}"
    fi
done

# 检查PJSIP
if $PKG_CONFIG_AVAILABLE && pkg-config --exists libpjproject 2>/dev/null; then
    echo "找到PJSIP (libpjproject)..."
    CFLAGS="$CFLAGS $(pkg-config --cflags libpjproject)"
    LIBS="$LIBS $(pkg-config --libs libpjproject)"
else
    echo "未找到PJSIP的pkg-config，使用默认包含路径..."
    CFLAGS="$CFLAGS -DPJ_AUTOCONF=1 -DPJ_IS_BIG_ENDIAN=0 -DPJ_IS_LITTLE_ENDIAN=1 -I/usr/local/include"
    LIBS="$LIBS -L/usr/local/lib -lpjsua2 -lstdc++ -lpjsua -lpjsip-ua -lpjsip-simple -lpjsip -lpjmedia-codec -lpjmedia-videodev -lpjmedia-audiodev -lpjmedia -lpjnath -lpjlib-util -lpj"
fi

# 创建输出目录
mkdir -p direct_build

# 收集源文件
if [ -d "src" ]; then
    SOURCE_FILES="src/*.cpp"
    INCLUDE_DIRS="-I. -Isrc"
else
    SOURCE_FILES="*.cpp"
    INCLUDE_DIRS="-I."
fi

# 构建编译命令
COMPILE_CMD="g++ -std=c++11 $INCLUDE_DIRS $CFLAGS $SOURCE_FILES -o direct_build/gb_client $LIBS -lpthread -lm -lstdc++ -lrt"

# 在Windows下调整编译命令
if $IS_WINDOWS; then
    # 移除Windows下不需要的库
    COMPILE_CMD=${COMPILE_CMD//-lrt/}
fi

# 显示编译命令
echo "使用以下命令编译:"
echo $COMPILE_CMD

# 执行编译
eval $COMPILE_CMD

# 检查编译结果
if [ $? -eq 0 ]; then
    echo "编译成功！"
    echo "现在可以运行: direct_build/gb_client"
    
    # 复制测试视频到输出目录
    if [ -f "test.mp4" ]; then
        cp test.mp4 direct_build/
        echo "已复制测试视频到direct_build目录"
    else
        echo "警告: 未找到test.mp4测试视频文件。"
        echo "请确保在运行程序的目录中有test.mp4文件。"
    fi
    
    # 创建一个符号链接或复制到当前目录
    if [ -f "direct_build/gb_client" ]; then
        if $IS_WINDOWS; then
            cp direct_build/gb_client ./gb_client
        else
            ln -sf direct_build/gb_client ./gb_client
        fi
        echo "已创建 gb_client 链接或副本在当前目录"
    fi
    
    # 检测测试视频
    if [ -f "test.mp4" ]; then
        echo "检测到 test.mp4 测试视频文件。"
    else
        echo "警告: 未找到测试视频文件。请确保在运行程序的目录中有test.mp4文件。"
    fi
else
    echo "编译失败！"
    exit 1
fi 